/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors:
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 * #asset(ui_mov/*) #asset(qx/mobile/icon/${qx.mobile.platform}/*)
 * #asset(qx/mobile/icon/common/*)
 * 
 ******************************************************************************/

/**
 * This is the main application class of your custom application "ui_mov"
 */
qx.Class.define("ui_mov.views.intereses", {
	extend : qx.ui.mobile.page.NavigationPage,
	construct : function() {
		this.base(arguments);
		this.set({
					title : "Seleccione Intereses", // will be replaced by
													// username
					showBackButton : true,
					backButtonText : "Volver"
				});
		var yes = "/static/img/y.png";
		var no = "/static/img/n.png";
		var list = new qx.ui.mobile.list.List({
					configureItem : function(item, data, row) {
						item.setImage(data.suscripted ? yes : no);
						item.setTitle(data.nombre);
						item.setSubtitle(data.descripcion);
						item.setSelectable(true);
					}
				});
		this.list = list;
		list.setModel(new qx.data.Array([]));
		// var busyIndicator = new
		// qx.ui.mobile.dialog.BusyIndicator("Cargando
		// Temas...");
		// qx.core.Init.getApplication().getRoot().add(busyIndicator);
		list.addListener("changeSelection", function(evt) {
					var tema = list.getModel().getItem(evt.getData());
					var accion = tema.suscripted ? 'borrar' : 'agregar';
					window.pages.busy.show();
					var req = new qx.io.request.Xhr("/rpc/intereses/" + accion);
					req.setRequestData({
								'idSubscriptor' : window.getSubscriptorId(),
								'idTema' : tema.idTema
							});
					req.addListener("success", function() {
								window.pages.busy.hide();
								this.refreshlist();
							}, this);
					req.send();
				}, this);
		this.addListener('start', this.refreshlist, this);

		this.addListener("back", function() {
					window.pages.welcome.show({
								reverse : true
							});
				}, this);
		this.add(list);
	},
	members : {
		refreshlist : function() {
			var req = new qx.io.request.Xhr("/rpc/intereses/listarTodos");
			window.pages.busy.show();
			req.setRequestData({
						'idSubscriptor' : window.getSubscriptorId()
					});
			req.addListener("success", function(e) {
						var res = e.getTarget().getResponse();
						this.list.setModel(new qx.data.Array(res));
						window.pages.busy.hide();
					}, this);
			req.send();
		}
	}
});
